(function(){
	var app = angular.module('projectRtc', [],
		function($locationProvider){$locationProvider.html5Mode(true);}
    );
	var client = new PeerManager();
	var mediaConfig = {
        audio:true,
        video: {
			mandatory: {},
			optional: []
        }
	};
	
	
     
	app.controller('RemoteStreamsController', [ '$http', function( $http){
		var rtc = this;
		rtc.remoteStreams = [];
		rtc.cols=3;
		rtc.phoneNums=10;
		
		rtc.getNumber=function(num){
			return new Array(num);
		}
		rtc.getCol=function(num){
			return new Array(num);
		}
		function getStreamById(id) {
		    for(var i=0; i<rtc.remoteStreams.length;i++) {
		    	if (rtc.remoteStreams[i].id === id) {return rtc.remoteStreams[i];}
		    }
		}
		rtc.loadData = function () {
			// get list of streams from the server
			$http.get('/streams.json').success(function(data){
				// filter own stream
				var streams = data.filter(function(stream) {
			      	return stream.id != client.getId();
			    });
			    // get former state
			    for(var i=0; i<streams.length;i++) {
			    	var stream = getStreamById(streams[i].id);
			    	//streams[i].isPlaying = (!!stream) ? stream.isPLaying : false;
					client.peerInit(streams[i].id,i);
					streams[i].isPlaying = !streams[i].isPlaying;
			    }
			    // save new streams
			    rtc.remoteStreams = streams;
			});
		};
 
		//initial load
		rtc.loadData();
    	 
	}]); 
})();
